package com.zwr.sheller.core.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zwr.sheller.core.common.BusinessException;
import com.zwr.sheller.core.common.ExceptionCode;
import com.zwr.sheller.core.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.List;

/**
 * @Author LSY
 * @Date 2021/11/10 11:05
 * @Version 1.0
 **/
public abstract class BaseServiceImpl<T> implements BaseService<T> {

    @Autowired(required = false)
    Mapper<T> mapper;

    @Override
    public PageInfo<T> selectTAll(Integer page, Integer num) {
        PageHelper.startPage(page, num);
        List<T> ts = mapper.selectAll();
        return PageInfo.of(ts);
    }

    @Override
    public PageInfo<T> selectAllOrVague(Integer page, Integer num, T... t) throws IllegalAccessException, BusinessException {
        PageHelper.startPage(page, num);
        List<T> list = null;
        if(t.length == 0) {
            list = mapper.selectAll();
        }
        else if(t.length == 1) {
            Class<?> clazz = t[0].getClass();
            Field[] fields = clazz.getDeclaredFields();

            Example example = new Example(clazz);
            Example.Criteria criteria = example.createCriteria();

            for(Field field : fields) {
                field.setAccessible(true);
                Object value = field.get(t[0]);
                if(value != null) {
                    criteria.orLike(field.getName(), "%"+value.toString()+"%");
                }
            }
            list = mapper.selectByExample(example);
        }
        else {
            throw new BusinessException(ExceptionCode.PARAMETER_PASSING_EXCEPTION);
        }
        return PageInfo.of(list);
    }

    @Override
    public T selectByPk(Serializable id) throws BusinessException {
        T t = mapper.selectByPrimaryKey(id);
        if(t == null) {
            throw new BusinessException(ExceptionCode.NO_MATCHING_RESULT);
        }
        return t;
    }

    @Override
    public PageInfo<T> selectByT(Integer page, Integer num, T t) {
        PageHelper.startPage(page, num);
        List<T> ts = mapper.select(t);
        return PageInfo.of(ts);
    }

    @Override
    public T insertTSelective(T t) throws BusinessException {
        int result = mapper.insertSelective(t);
        if(result < 1) {
            throw new BusinessException(ExceptionCode.INSERT_FAILED);
        }
        return t;
    }

    @Override
    public void deleteTByPk(Serializable id) throws BusinessException {
        int result = mapper.deleteByPrimaryKey(id);
        if(result < 1) {
            throw new BusinessException(ExceptionCode.DELETE_FAILED);
        }
    }

    @Override
    public void updateTByNewT(T t) throws BusinessException {
        int result = mapper.updateByPrimaryKeySelective(t);
        if(result < 1) {
            throw new BusinessException(ExceptionCode.UPDATE_FAILED);
        }
    }

    @Override
    public PageInfo<T> selectTByExample(Integer page, Integer num, Example example) {
        PageHelper.startPage(page, num);
        List<T> ts = mapper.selectByExample(example);
        return PageInfo.of(ts);
    }

    @Override
    public List<T> selectAll(){
        List<T> list = mapper.selectAll();

        return list;
    }

    @Override
    public T selectByFeatures(T t){
        T t1 = mapper.selectOne(t);
        return t1;
    }
    @Override
    public PageInfo<T> selectAll(Integer page,Integer size, Example example){
        PageHelper.startPage(page,size);
        List<T> list = mapper.selectByExample(example);
        return PageInfo.of(list);
    }
    @Override
    public Integer updateByNewT(T t,Example example) throws BusinessException {
        int i = mapper.updateByExampleSelective(t, example);
        if (i!=1){
            throw new BusinessException(ExceptionCode.UPDATE_FAILED);
        }
        return i;
    }


}
